package com.googlecode.imanager.client.action;

import com.googlecode.imanager.client.Application;
import com.googlecode.imanager.client.Control;
import com.googlecode.imanager.client.bridge.BridgeManager;
import com.googlecode.imanager.client.io.HttpRequest;
import com.googlecode.imanager.client.io.Path;
import com.googlecode.imanager.client.io.HttpRequest.RequestType;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class HiperLinkAction extends Action {

    private static final long serialVersionUID = 1L;

    private Path path;
    private HiperLinkTarget target;
    
    public HiperLinkAction() {
        target = HiperLinkTarget.DEFAULT;
    }
    
    @Override
    protected void doExecute(Control sender) {
        if (BridgeManager.getInstance().executeAction(this, sender)){
            // Action handled by bridge implementation
            return;
        }
        
        RequestType requestType;
        
        if (target == HiperLinkTarget.BLANK){
            requestType = RequestType.BLANK_PAGE;
        } else {
            requestType = RequestType.SUBMIT;
        }
        
        Application app = sender.getApplication();
        if (app == null){
            throw new IllegalStateException("no application is running");
        }
        
        app.httpCall(new HttpRequest(path, "GET", requestType, null, null));
    }

    public Path getPath() {
        return path;
    }

    public void setPath(Path path) {
        this.path = path;
    }

    public HiperLinkTarget getTarget() {
        return target;
    }

    public void setTarget(HiperLinkTarget target) {
        this.target = target;
    }
    
}
